package util;

import java.io.IOException;
import java.net.URL;
import java.util.List;

import com.google.gdata.client.contacts.ContactsService;
import com.google.gdata.data.contacts.ContactEntry;
import com.google.gdata.data.contacts.ContactFeed;
//import com.google.gdata.data.extensions.Email;
//import com.google.gdata.data.extensions.Name;
//import com.google.gdata.data.extensions.PhoneNumber;
import com.google.gdata.util.PreconditionFailedException;
import com.google.gdata.util.ServiceException;

public class ContactUtil {

	public ContactUtil() {
	}

	public static List<ContactEntry> retrieveAllContacts(String strEmail,
			String strPassword) {
		List<ContactEntry> result = null;

		ContactsService myService = new ContactsService("BuzzCommunication");
		try {
			myService.setUserCredentials(strEmail, strPassword);

			URL feedUrl = new URL(
					"https://www.google.com/m8/feeds/contacts/default/full?max-results=1000");

			ContactFeed resultFeed = myService.getFeed(feedUrl,
					ContactFeed.class);
			result = resultFeed.getEntries();
		} catch (ServiceException ex) {
			System.out.println("Error in getting contact! + \n\t"
					+ ex.toString());
		} catch (IOException ex) {
			System.out.println("Error in getting contact! + \n\t"
					+ ex.toString());
		}

		return result;
	}

	public static boolean delete(String strEmail, String strPassword,
			String strContactId) {
		boolean deletedResult = true;
		ContactsService myService = new ContactsService("BuzzCommunication");
		try {
			myService.setUserCredentials(strEmail, strPassword);

			URL contactURL = new URL(
					"https://www.google.com/m8/feeds/contacts/default/full/"
							+ strContactId);
			ContactEntry contact = myService.getEntry(contactURL,
					ContactEntry.class);
			contact.delete();
		} catch (PreconditionFailedException ex) {
			// Etags mismatch: handle the exception.
			System.out.println("ERROR: deleting contact!\n\t" + ex.toString());
			deletedResult = false;
		} catch (ServiceException ex) {
			System.out.println("ERROR: deleting contact!\n\t" + ex.toString());
			deletedResult = false;
		} catch (IOException ex) {
			System.out.println("ERROR: deleting contact!\n\t" + ex.toString());
			deletedResult = false;
		}

		return deletedResult;
	}

	public static boolean update(String strEmail, String strPassword,
			String strContactId) {
		return true;
	}
}
